<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Incoming;


use App\Api\Controllers\BaseController;
use App\Models\IncomingQrs;
use App\Traits\ResponseTrait;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use BaconQrCode\Renderer\Image;
use Illuminate\Http\Request;

class QrController extends BaseController
{

    use ResponseTrait;

    //二维码列表
    public function listQr(Request $request): string
    {

        $page = $request->input("page", 1);
        $pagesize = $request->input("pagesize", 10);


        $user = $this->parseToken();

        $query = IncomingQrs::query()->where("user_id", $user->user_id);

        $lists = $query->offset(($page - 1) * $pagesize)->limit($pagesize)->get()->toArray();

        $total = IncomingQrs::query()->where("user_id", $user->user_id)->count();

        $data = [
            "total" => $total,
            "data" => $lists,
            "page" => $page
        ];

        return $this->successResponse('ok', $data);
    }

    public function createQr(Request $request): string
    {
        try {
            $user = $this->parseToken();
            //生成的批次
            $cno = date("Ymdhis", time()) . rand(1000, 9999);
            //生成数量
            $num = $request->get('num', 20);
            if ($num > 20) {
                throw new \Exception("生成数量控制在20以内");
            }
            $data = [];
            for ($i = 1; $i <= $num; $i++) {
                $code_number = 'IncomingNO_' . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));//编号
                $url = url('/qrIncoming?user_id=' . $user->user_id . '&code_number=' . $code_number);//生成的url准备生成二维码;
                try {
                    //生成二维码文件
                    $path = public_path('QrCodeIncoming/' . $cno . '/');
                    if (!is_dir($path)) {
                        mkdir($path, 0777, true);
                    }
                    $renderer = new ImageRenderer(new RendererStyle(400), new Image\ImagickImageBackEnd());
                    $writer = new Writer($renderer);
                    $file_path = $path . $code_number . '.png';// public_path('QrCodeIncoming/' . $cno . '/' . $code_number . '.png');
                    $writer->writeFile($url, $file_path);
                } catch (\Exception $exception) {
                    throw new \Exception($exception->getMessage());
                }
                $tmp = [
                    "user_id" => $user->user_id,
                    "code_number" => $code_number,
                    "code_type" => 0,
                    "url" => $url,
                    "path" => url('QrCodeIncoming/' . $cno . '/' . $code_number . '.png'),
                    "store_id" => "",
                    "created_at" => time(),
                    "updated_at" => time(),
                    "search_code" => md5(date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 9999)))
                ];
                $data[] = $tmp;
            }
            IncomingQrs::query()->insert($data);
            return $this->successResponse("生成二维码成功");
        } catch (\Exception $exception) {
            return $this->successResponse($exception->getMessage());
        }
    }


}